package com.woniu.his.controller;

import com.woniu.his.entity.*;
import com.woniu.his.service.OutpatientService;
import com.woniu.his.service.impl.OutpatientServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/outpatient")
//@Slf4j
public class OutpatientController {
    @Autowired
    private OutpatientService outpatientService = new OutpatientServiceImpl();

    @RequestMapping("index")
    @RequiresRoles("门诊部")
    public String home(ModelMap modelMap) {
        List<HashMap> registrations = outpatientService.queryAllRegistration();
        modelMap.addAttribute("registrations", registrations);
        return "outpatient-patients";
    }

    /*
    异常测试类
     */
    @RequestMapping("errorTest")
    @ResponseBody
    public String errorTest() {
        int i = 1 / 0;
        return "outpatient-patients";
    }

    /*
    预约
     */
    @RequestMapping("appointments")
    @RequiresRoles("门诊部")
    public String appointments(){
        return "outpatient-appointments";
    }

    /*
    患者排号列表
     */
    @RequestMapping("patients")
    @RequiresRoles("门诊部")
    public String patients(ModelMap modelMap) {
        List<HashMap> registrations = outpatientService.queryAllRegistration();
        modelMap.addAttribute("registrations", registrations);
        return "outpatient-patients";
    }

    /*
    患者信息
     */
    @RequestMapping("patientInfo")
    @RequiresRoles("门诊部")
    public String patientInfo(){
        return "outpatient-patient-info";
    }

    /*
    药品申请页面
     */
    @RequestMapping("drugApplyShow")
    @RequiresRoles("门诊部")
    public String drugApplyShow(){
        return "outpatient-drug-apply";
    }

    /*
    查询药品详情
     */
    @RequestMapping("queryDrugInfo")
    @ResponseBody
    @RequiresRoles({"门诊部", "住院部"})
    public List<DrugInfo> queryDrugInfo(){
        return outpatientService.queryDrugInfo();
    }

    /*
    手术申请
     */
    @RequestMapping("operationApply")
    @RequiresRoles("门诊部")
    public String operationApply(){
        return "outpatient-operation-apply";
    }

    /*
    住院申请
     */
    @RequestMapping("inHospitalApply")
    @RequiresRoles("门诊部")
    public String inHospitalApply(){
        return "outpatient-operation-apply";
    }

    /*
    检查申请
     */
    @RequestMapping("examineApply")
    @RequiresRoles("门诊部")
    public String examineApply(){
        return "outpatient-operation-apply";
    }

    /*
    就诊信息提交
     */
    @PostMapping ("savepatientInfo")
    @RequiresRoles("门诊部")
    public String savepatientInfo(PatientInfo patientInfo, RegistrationInfo registrationInfo,
                                  MedicalInfo medicalInfo){

        System.out.println("挂号患者: " + registrationInfo);
        System.out.println("获取到的患者实体类:" + patientInfo);
        System.out.println("诊断详情:" + medicalInfo);
        Date date = new Date();
        //患者建档(患者数据),返回患者id
        patientInfo.setRegisterTime(date);
        int patientId = outpatientService.savepatientInfo(patientInfo);
        System.out.println("新增后返回的患者id: " + patientId);
//        log.info("挂号患者:{}",registrationInfo);

        //挂号id
        Integer registrationId = registrationInfo.getRegistrationId();
        //接诊科室id
        Integer registrationSectionId = registrationInfo.getRegistrationSectionId();
        //接诊医生id
        Integer registrationDocterId = registrationInfo.getRegistrationDocterId();

        //构建诊断信息字段
        medicalInfo.setPatientId(patientId);
        medicalInfo.setSectionId(registrationSectionId);
        medicalInfo.setDocterId(registrationDocterId);
        medicalInfo.setMedicalTime(date);

        //保存诊断信息, 返回就诊信息id
        int medicalId = outpatientService.saveMedicalInfo(medicalInfo);

        //修改挂号表中数据状态为2
        outpatientService.updateRegisStatus(registrationId,2);

        //根据治疗建议进行下一步操作
        Integer status = medicalInfo.getStatus();
        //住院实体对象
        HospitalizationInfo hpi = new HospitalizationInfo();
        hpi.setMedicalId(medicalId);
        hpi.setSectionId(registrationSectionId);
        //手术实体对象
        OperationInfo operationInfo = new OperationInfo();
        operationInfo.setMedicalId(medicalId);
        operationInfo.setOperationSectionId(registrationSectionId);
        if (status == 1){
            //开药

        }else if (status == 2){
            //住院
            outpatientService.addHospitalizationInfo(hpi);
        }else if (status == 3){
            //手术
            outpatientService.addOperationInfo(operationInfo);
        }else if (status == 4){
            //手术及住院
            outpatientService.addHospitalizationInfo(hpi);
            outpatientService.addOperationInfo(operationInfo);
        }else {
            //其他
        }
        return "outpatient-patient-info";
    }

    /*
    药品申请表
     */
    @RequestMapping("drugApply")
    @ResponseBody
    @RequiresRoles("门诊部")
    public List<Map<String, Object>> drugApply(){
        //查询所有需要开药的患者
        List<Map<String, Object>> medicalInfos = outpatientService.queryDrugMedical();
        System.out.println("需要开药的患者:" + medicalInfos);
        return medicalInfos;
    }

    /*
    添加药品申请
     */
    @PostMapping("addDrugApply")
    @ResponseBody
    @RequiresRoles({"门诊部", "住院部"})
    public String addDrugApply(@RequestBody List<Map> drugApplyList){
        if (drugApplyList.size() > 0){
            //循环数据添加药品申请
            for (int i=0; i<drugApplyList.size(); i++){
                Map drugApplyMap = drugApplyList.get(i);
                outpatientService.addDrugApply(drugApplyMap);
            }
            //修改诊断表状态为药品申请单填写完成
            outpatientService.updateMedicalStatus(drugApplyList.get(0).get("medicalId"));
        }
        System.out.println("获取到的drugApplies: " + drugApplyList);
//        return "outpatient-drug-apply";
        return "处理完成";
    }


    /*
    添加药品申请(住院部)
     */
    @PostMapping("addDrugApply1")
    @ResponseBody
    @RequiresRoles({"门诊部", "住院部"})
    public String addDrugApply1(@RequestBody List<Map> drugApplyList) {
        if (drugApplyList.size() > 0) {
            //循环数据添加药品申请
            for (int i = 0; i < drugApplyList.size(); i++) {
                Map drugApplyMap = drugApplyList.get(i);
                outpatientService.addDrugApply(drugApplyMap);
            }
        }
        System.out.println("获取到的drugApplies: " + drugApplyList);
//        return "outpatient-drug-apply";
        return "处理完成";
    }

    /*
    查询所有挂号患者
     */
    @GetMapping("queryAllRegistration")
    @ResponseBody
    @RequiresRoles("门诊部")
    public List<HashMap> queryAllRegistration(){
        List<HashMap> registrations = outpatientService.queryAllRegistration();
        return registrations;
    }

    /*
    修改挂号患者状态
     */
    @PostMapping("updateRegStatus")
    @ResponseBody
    @RequiresRoles("门诊部")
    public String updateRegStatus(String registrationId){
        System.out.println("______________________" + registrationId);
        int i = outpatientService.updateRegStatus(registrationId);
        return "" + i;
    }

    /*
    查询就诊完成的患者
     */
    @GetMapping("queryRegis")
    @ResponseBody
    @RequiresRoles("门诊部")
    public List<RegistrationInfo> queryRegis(){
        return outpatientService.queryRegis();
    }
}
